package schudel.daniel.androidspellingquiz;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class ResultsActivity extends Activity implements OnTouchListener {
    private SpellingQuizApplication app;
    private TextView textViewAnswers;
    private static final String TAG = "ResultsActivity: ";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_results);
    	app = (SpellingQuizApplication) this.getApplication();
    	textViewAnswers = (TextView)findViewById(R.id.textViewAnswers);
    }

    protected void onStart() {
    	super.onStart();
        Log.d(TAG, "onStart");

        setTitle("Quiz Results for the list \"" + app.getCurrentListName() + "\"");
        tabulateResults();
    }
    
    protected void onRestart() {
        Log.d(TAG, "onRestart");
    	super.onRestart();
    }

    protected void onResume() {
        Log.d(TAG, "onResume");
    	super.onResume();
    }

    protected void onPause() {
        Log.d(TAG, "onPause");
    	super.onPause();
    }

    protected void onStop() {
        Log.d(TAG, "onStop");
    	super.onStop();
    }

    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
    	super.onDestroy();
    }
   
    private void tabulateResults() {

    	String correct = "";
    	String incorrect = "";

    	ArrayList<String> words= app.getCurrentWords();
    	ArrayList<String> answers = app.getAnswers();

    	int i = 0;
    	while (i != words.size()) {
    		String word = words.get(i);
    		String answer = answers.get(i);
    		if (word.equals(answer)) {
    			correct += word + System.getProperty("line.separator");
    		} else {
    			incorrect += word + " (" + answer + ")" + System.getProperty("line.separator");
    		}
    		++i;
    	}
    	String display = "";
    	if (incorrect.length() == 0) {
    		display = "You spelled all words correctly!" + System.getProperty("line.separator") + correct;
    	} else {
    		if (correct.length() != 0) {
    			display = "You spelled these words correctly!" + System.getProperty("line.separator") + correct;
    		}
    		display += "Practice these words more:" + System.getProperty("line.separator") + incorrect;
    	}
    	textViewAnswers.setText(display);
    	textViewAnswers.setMovementMethod(new ScrollingMovementMethod());
    }

    public boolean onTouch(View view, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_BACK:
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
            return true;
        default:
            return(super.onKeyDown(keyCode, event));
        }
    }
}
